# -*- coding: utf-8 -*-
import pandas as pd
from IQEngine.api import *
from qtapi.api import *
import datetime


# 在使用模型研究编写策略前，请详细阅读说明文档

def initialize(context):
    context.s1 = "600570.XSHG"
    context.v1 = 1
    set_universe(context.s1)
    context.job2_counter = 0
    context.job1_counter = 0
    context.threads = []
    # print('op', get_opinstance())
    run_interval(context, job1, seconds=10, starttime=93000, endtime=230000)
    # run_interval(context, job2, seconds=1, starttime=93000, endtime=140000)
    # print('snap', get_snapshots(['000001.XSHE']))


def handle_data(context, bar_dict):
    print('got bar', bar_dict)


def on_trade_response(context, trade_resp):
    print('on trade response, context', trade_resp)


def on_order_response(context, order_resp):
    print('on order response', order_resp)


def handle_tick(context, bar_dict):
    print(datetime.datetime.now(), 'got tick', bar_dict.keys())


def job1(context, ):
    context.job1_counter += 1
    print("available money", get_available_money(705))
    print("get_min_price_interval", get_min_price_interval(["600570.XSHG", "512880.XSHG"]))
    print("get_trader_time", get_trader_time("600570.XSHG"))
    print(datetime.datetime.now(), 'run job1', context.job1_counter)
    if context.job1_counter <= 2:
        entrust_list = []
        investunit_id = 705
        instance_id = 186
        item1 = {"symbol": '600570.XSHG',  # 代码
                 "price": 78.00,  # 委托价格
                 "amount": 100,  # 委托数量
                 "entrust_direction": 'BUY',  # 买卖方向
                 "invest_type": "1",  # 投资类型
                 "investunit_id": investunit_id,
                 "instance_id": instance_id}
        entrust_list.append(item1)  # 篮子的第一个券
        item2 = {"symbol": '600570.XSHG',  # 代码
                 "price": 78.00,  # 委托价格
                 "amount": 100,  # 委托数量
                 "entrust_direction": 'BUY',  # 买卖方向
                 "invest_type": "1",  # 投资类型
                 "investunit_id": investunit_id,
                 "instance_id": instance_id}
        entrust_list.append(item2)  # 篮子的第二个券
        order_id_list = order_basket(entrust_list)
        print(order_id_list)


def job2(context, ):
    context.job2_counter += 1
    print(datetime.datetime.now(), 'run job2', context.job2_counter)
